home *** CD-ROM | disk | FTP | other *** search
Text File | 2000-09-28 | 12.6 KB | 520 lines | [TEXT/CWIE] |
- /*
- File: TestGXToPostScript.cp
-
- Contains: QuickDraw GX to PostScript conversion code.
-
- Version: Technology: Quickdraw GX 1.1.x
-
- Copyright: ©1997 by Apple Computer, Inc., all rights reserved.
- */
-
- #include <Errors.h>
- #include <Files.h>
- #include <GXGraphics.h>
- #include <StorageLibrary.h>
- #include <GraphicsLibraries.h>
-
- #include "GXPrintingUniverse.h"
- #include <Dialogs.h>
- #include <Controls.h>
- #include <StandardFile.h>
-
-
- #define kPreviewItem 4
- #define kNoPreview 1
- #define kBlackAndWhitePreview 2
- #define k8BitPreview 3
- #define k32BitPreview 4
-
- #define kLevelFirstItem 5
- #define kLevelButtonCount 2
- #define kPSLevel1 1
- #define kPSLevel2 2
-
-
- #define kFontFirstItem 8
- #define kFontButtonCount 2
- #define kPSOnlyFonts 1
- #define kPSPlusTTFonts 2
-
-
- #define kColorFirstItem 11
- #define kColorButtonCount 3
- #define kGrayColors 1
- #define kRGBColors 2
- #define kCMYKColors 3
-
-
- #define kPointFirstItem 15
- #define kPointButtonCount 3
- #define k1500points 1
- #define k3000points 2
- #define k10000points 3
-
-
- #define kDataFirstItem 19
- #define kDataButtonCount 2
- #define kAscii 1
- #define kBinary 2
-
- #define kVMItem 22
- #define k250kb 1
- #define k500kb 2
- #define k1000kb 3
- #define k2000kb 4
- #define kUnlimittedVM 5
-
- typedef struct {
-
- long version;
- long previewType;
- long languageLevel;
- long fontFormats;
- long colorSpace;
- long pointLimit;
- long dataFormat;
- long maxVM;
-
- } TepsSettings;
-
- /*****************************************
-
- SetRadioButtonOn:
-
- You'd think there would be a routine like
- this in the tool box. What a pain.
-
- Oh well.
-
- Routine turns on a radio button in the specified
- range, and turns off the others.
-
- dPtr: Dialog pointer.
- firstItem: index of first item in group of radio buttons. (index of staticText item
- right before buttons)
- itemCount: How many radio buttons in group.
- value: Which one to turn on.
-
- *******************************************/
- void SetRadioButtonOn( DialogPtr dPtr, short firstItem, short itemCount, short value);
- void SetRadioButtonOn( DialogPtr dPtr, short firstItem, short itemCount, short value)
- {
- short idx;
- short itemType;
- Rect itemRect;
- Handle hItem;
-
- for (idx = 1; idx <= itemCount; ++idx) {
-
- GetDialogItem(dPtr, firstItem + idx, &itemType, &hItem, &itemRect);
-
- if (idx != value)
- SetControlValue((ControlHandle)hItem, 0);
- else
- SetControlValue((ControlHandle)hItem, 1);
-
- }//end for
-
- }//SetRadioButtonOn
-
- /************************************************
-
- HandleSettingsDialog:
-
- Routine controls the DTP dialog for setting up
- the extension options.
-
- *************************************************/
- void HandleSettingsDialog(DialogPtr dPtr, short *hit, TepsSettings *useSettings);
- void HandleSettingsDialog(DialogPtr dPtr, short *hit, TepsSettings *useSettings)
- {
- OSErr status;
- short itemType;
- Rect itemRect;
- Handle hItem;
- TepsSettings settings, newSettings;
- short itemHit;
-
- settings.version = 0;
- settings.previewType = kNoPreview;
- settings.languageLevel = kPSLevel1;
- settings.fontFormats = kPSOnlyFonts;
- settings.colorSpace = kRGBColors;
- settings.pointLimit = k1500points;
- settings.dataFormat = kAscii;
- settings.maxVM = k250kb;
-
- newSettings = settings;
-
- /** Initialize settings based on what's stored in DTP **/
-
- // Preview popup.
- GetDialogItem(dPtr, kPreviewItem, &itemType, &hItem, &itemRect);
- SetControlValue((ControlHandle)hItem, settings.previewType);
-
- // VM Usage popup.
- GetDialogItem(dPtr, kVMItem, &itemType, &hItem, &itemRect);
- SetControlValue((ControlHandle)hItem, settings.maxVM);
-
- // The rest.
- SetRadioButtonOn( dPtr, kLevelFirstItem , kLevelButtonCount, newSettings.languageLevel);
- SetRadioButtonOn( dPtr, kFontFirstItem , kFontButtonCount, newSettings.fontFormats);
- SetRadioButtonOn( dPtr, kColorFirstItem , kColorButtonCount, newSettings.colorSpace);
- SetRadioButtonOn( dPtr, kPointFirstItem , kPointButtonCount, newSettings.pointLimit);
- SetRadioButtonOn( dPtr, kDataFirstItem , kDataButtonCount, newSettings.dataFormat);
-
- do { /* Loop for dialog handling */
-
- ModalDialog(nil, hit);
-
- itemHit = *hit;
-
- if ( itemHit == kPreviewItem ) {
-
- GetDialogItem(dPtr, kPreviewItem, &itemType, &hItem, &itemRect);
- newSettings.previewType = GetControlValue((ControlHandle)hItem);
-
- } else if (itemHit == kVMItem) {
-
- GetDialogItem(dPtr, kVMItem, &itemType, &hItem, &itemRect);
- newSettings.maxVM = GetControlValue((ControlHandle)hItem);
-
- } else if ( (itemHit > kLevelFirstItem) && ( itemHit <= kLevelFirstItem + kLevelButtonCount) ) {
-
- newSettings.languageLevel = itemHit - kLevelFirstItem;
- SetRadioButtonOn( dPtr, kLevelFirstItem, kLevelButtonCount, newSettings.languageLevel);
-
-
- } else if ( (itemHit > kFontFirstItem) && ( itemHit <= kFontFirstItem + kFontButtonCount) ) {
-
- newSettings.fontFormats = itemHit - kFontFirstItem;
- SetRadioButtonOn( dPtr, kFontFirstItem, kFontButtonCount, newSettings.fontFormats);
-
-
- } else if ( (itemHit > kColorFirstItem) && ( itemHit <= kColorFirstItem + kColorButtonCount) ) {
-
- newSettings.colorSpace = itemHit - kColorFirstItem;
- SetRadioButtonOn( dPtr, kColorFirstItem, kColorButtonCount, newSettings.colorSpace);
-
-
- } else if ( (itemHit > kPointFirstItem) && ( itemHit <= kPointFirstItem + kPointButtonCount) ) {
-
- newSettings.pointLimit = itemHit - kPointFirstItem;
- SetRadioButtonOn( dPtr, kPointFirstItem, kPointButtonCount, newSettings.pointLimit);
-
-
- } else if ( (itemHit > kDataFirstItem) && ( itemHit <= kDataFirstItem + kDataButtonCount) ) {
-
- newSettings.dataFormat = itemHit - kDataFirstItem;
- SetRadioButtonOn( dPtr, kDataFirstItem, kDataButtonCount, newSettings.dataFormat);
-
- }//end if
-
- } while ( (itemHit != ok) && (itemHit != cancel));
-
- *useSettings = newSettings;
-
- }//HandleSettingsDialog
-
-
- /************************************************************************************/
-
- Boolean DoSettingsDialog(gxPostScriptImageDataRec *imageData);
- Boolean DoSettingsDialog(gxPostScriptImageDataRec *imageData)
- {
- DialogRef dlog;
- short hit;
- short oldResFile;
- GrafPtr oldPort;
- TepsSettings theSettings;
-
- GetPort(&oldPort);
-
- dlog = GetNewDialog(128, nil, (WindowRef)-1);
-
- SetDialogDefaultItem(dlog, ok);
- SetDialogCancelItem(dlog, cancel);
-
- ShowWindow((WindowRef)dlog);
- SetPort(dlog);
-
- HandleSettingsDialog(dlog, &hit, &theSettings);
-
- SetPort(oldPort);
-
- DisposeDialog(dlog);
-
-
- OSErr status;
- gxPostScriptRenderOptions options;
- scalerStreamTypeFlag streamTypes;
- gxPostScriptImageDataPtr pImageData;
-
- options = gxEPSTargetOption;
-
- streamTypes = type1StreamType +
- type3StreamType +
- portableStreamType +
- evenOddModifierStreamType;
-
- if (theSettings.dataFormat == kAscii)
- options += gxNeedsHexOption;
-
- if (theSettings.fontFormats == kPSPlusTTFonts)
- streamTypes += type42StreamType;
-
- /** Get Language Level **/
- if (theSettings.languageLevel == kPSLevel1) {
- imageData->languageLevel = 1;
- options += gxPortablePostScriptOption;
- } else if (theSettings.languageLevel == kPSLevel2) {
- imageData->languageLevel = 2;
- }//end if
-
-
- imageData->renderOptions = options;
- imageData->fontType = streamTypes;
-
- /** Deal with color fields **/
- imageData->devCProfile = nil;
-
- switch (theSettings.colorSpace) {
-
- case kGrayColors:
- imageData->devCSpace = gxGraySpace;
- break;
-
- case kRGBColors:
- imageData->devCSpace = gxRGBSpace;
- break;
-
- case kCMYKColors:
- imageData->devCSpace = gxCMYKSpace;
- break;
-
- }//end switch
-
- /** Deal with path limit **/
- switch (theSettings.pointLimit) {
-
- case k1500points:
- imageData->pathLimit = 1500;
- break;
-
- case k3000points:
- imageData->pathLimit = 3000;
- break;
-
- case k10000points:
- imageData->pathLimit = 10000;
- break;
-
- }//end switch
-
- /** Deal with maxVM **/
- switch (theSettings.maxVM) {
-
- case k250kb:
- imageData->printerVM = 250 * 1024;
- break;
-
- case k500kb:
- imageData->printerVM = 500 * 1024;
- break;
-
- case k1000kb:
- imageData->printerVM = 1000 * 1024;
- break;
-
- case k2000kb:
- imageData->printerVM = 2000 * 1024;
- break;
-
- case kUnlimittedVM:
- imageData->printerVM = 0x7FFFFFFF;
- break;
-
- }//end switch
-
-
- /* Remaining things are hard-coded */
-
- imageData->gsaveLimit = 25; // Allow 6 for app embedding EPS file.
- imageData->opStackLimit = 450; // Allow 50 for app embedding EPS file.
-
- return (hit == ok) ? true : false;
-
- }
-
-
- gxShape ShapeRead();
- gxShape ShapeRead()
- {
- OSErr status;
- gxShape result = nil;
- StandardFileReply reply;
- OSType theTypes[1] = {'qdgx'};
- GrafPtr thecurPort;
- GetPort(&thecurPort);
-
- StandardGetFile(nil, 1, theTypes, &reply);
-
- SetPort(thecurPort);
-
- if (reply.sfGood) {
-
- short refnum;
- status = FSpOpenDF(&reply.sfFile, fsRdPerm, &refnum);
-
- result = FRefToShape(refnum, 0, nil);
-
- status = FSClose(refnum);
-
- }//end if
-
- return(result);
-
- }//ShapeRead
-
-
-
- #include <Stdio.h>
- #include <Files.h>
- #include "GXToPostScript.h"
- #include "IOUtilities.h"
-
- // Define a subclass of the PostScript device for our test I/O
- class testDevice : public CGXtoPostScriptDevice {
-
- public:
- testDevice(short refNum) {mRefnum = refNum; mSize = 0; mTotalSize = 0;};
- virtual ~testDevice() {
- mTotalSize += mSize;
- printf("Done, total file size: %ld\n", mTotalSize);
- };
-
- virtual OSErr BufferData(char* data, long size, unsigned long flags)
- {
- OSErr status = noErr;
- char* outputData = data;
- if (flags & gxMakeBufferHex) {
- outputData = new char[size * 2];
- HexBlockMove((unsigned char*)data, (unsigned char*)outputData, size);
- size *= 2;
- }//end if
-
- long count = size;
- status = FSWrite(mRefnum, &count, outputData);
- mSize += count;
- mTotalSize += count;
- if (mSize > 10240) {
- mSize = 0;
- printf("Wrote %ld bytes\n", mTotalSize);
- }//end if
-
- if (outputData != data)
- delete [] outputData;
-
- return status;
-
- };
-
- virtual OSErr Idle() {return noErr;};
-
- long GetSize() {return mTotalSize;};
-
- protected:
- short mRefnum;
- long mSize, mTotalSize;
- };
-
- short DoSave();
- short DoSave()
- {
- SFReply reply;
- OSErr status = noErr;
- Point where = {50, 50};
- SFPutFile(where, "\pSave Garnet Stream", "\pTest.ps", nil, &reply);
- short refnum;
- if (reply.good) {
-
- status = Create(reply.fName, reply.vRefNum, 'MPS ', 'TEXT');
- if ((status == noErr) || (status == dupFNErr)) {
- status = FSOpen(reply.fName, reply.vRefNum, &refnum);
- }//end if
- }//end if
-
- if (status != noErr)
- throw status;
-
- return refnum;
- }
-
- main() {
-
- // This ensures that new throws an exception if there is no space.
- extern char __throws_bad_alloc;
- long throwvalue = 1;
- __throws_bad_alloc = throwvalue;
-
- InitGraf(&qd.thePort); InitFonts(); InitWindows();
- InitMenus(); TEInit(); InitDialogs(nil); InitCursor();
- SetPort(qd.thePort);
- GXEnterGraphics();
-
- SetGraphicsLibraryErrors();
-
- gxPostScriptImageDataRec imageData;
- gxMapping devMapping;
-
- // Initialize the input parameters.
- ResetMapping(&devMapping);
- ScaleMapping(&devMapping, FloatToFixed(300.0/72.0), FloatToFixed(300.0/72.0), 0, 0);
-
- if (DoSettingsDialog(&imageData)) {
-
- gxShape aShape = ShapeRead(); // Read in a GX shape from a file.
- short refNum = DoSave(); // Create the PostScript file.
- TFontDbase theDbase = nil; // Nil font database causes page by page font handling.
-
- try {
- testDevice theDevice(refNum);
- if (aShape != nil) {
- try {
-
- /* Now image the page. Note, pass in nil for the font database to cause page by page font handling.*/
-
- // Construct the translator object.
- CGXtoPostScriptTranslator theTranslator(&theDevice, &imageData, theDbase, &devMapping, nil);
-
- // Embed the proc-sets in the PostScript strea.
- theTranslator.DownloadProcSets();
-
- // Normally the LW driver would take care of this during page-setup time.
- theDevice.BufferData("0 700 translate 1 -1 scale\n", 27, 0);
-
- // Image the page.
- theTranslator.InitGraphics(&imageData, &devMapping, nil);
- theTranslator.DrawShape(aShape, true);
- theTranslator.RestoreGraphics();
-
- // Normally the LW driver would take care of this at the end of the page.
- theDevice.BufferData("showpage\n", 9, 0);
-
- } catch(OSErr status) {
- printf("Error happend: %d\n", status);
- }// try-catch.
-
- }//end if
-
- // Close the file
- SetEOF(refNum, theDevice.GetSize());
- FSClose(refNum);
-
- } catch(OSErr theError) {
- printf("Error happend openning PS file: %d\n", theError);
- }//end try-catch
-
- }//end if
- }
-